package com.example.outdoor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.CheckBox;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;

public class Verification extends Activity implements OnClickListener,

OnItemSelectedListener {
	Spinner spinner;
	Spinner StartingPointspinner;
	DatabaseHelper db;
	List<String> list;
	ArrayAdapter<String> adapter;
	List<String> listStartingPoint;
	ArrayAdapter<String> adapterStartingPoint;
	Button goForVerification, btnGoBackVerificationTables;
	int selectedPosition = 0;
	SharedPreferences pref;
	SharedPreferences prefss;
	int positionname = 0;
	CheckBox chkIo;
	SQLiteDatabase dbBill;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.verification);

		pref = getSharedPreferences("loginstatus", MODE_PRIVATE);
		prefss = getSharedPreferences("savedData111", MODE_PRIVATE);
		selectedPosition = pref.getInt("selectedPosition", selectedPosition);
		positionname = prefss.getInt("savedData111", positionname);

		db = new DatabaseHelper(Verification.this);
		spinner = (Spinner) findViewById(R.id.spnAvenue);
		StartingPointspinner = (Spinner) findViewById(R.id.spnStartingPoint);
		spinner.setOnItemSelectedListener(this);
		goForVerification = (Button) findViewById(R.id.btnGoForVerification);
		goForVerification.setOnClickListener(this);
		btnGoBackVerificationTables = (Button) findViewById(R.id.btnGoBackVerificationTables);
		btnGoBackVerificationTables.setOnClickListener(this);
		chkIo = ((CheckBox) findViewById(R.id.chkIos));
		chkIo.setOnClickListener(this);
		Intent intent = getIntent();

		loadSpinner();

	}

	private String continueWithVerification(String paramString) {

		String str1 = "";
		Cursor cursor = this.dbBill.rawQuery(
				"select * from tblavenue where AvenueDescription='"
						+ paramString + "'", null);
		cursor.moveToFirst();
		while (cursor.isAfterLast()) {
			str1 = cursor.getString(1);
			cursor.moveToNext();
		}
		cursor.close();
		String str2 = "";
		Cursor cur = dbBill
				.rawQuery(
						"SELECT *,max(id) FROM tblBillboardOutput where Avenue="
								+ str1, null);
		while (cur.isAfterLast()) {
			str2 = cur.getString(3);
			cur.moveToNext();
		}
		cur.close();
		String str3 = "";
		Cursor curs = dbBill.rawQuery(
				"SELECT * FROM tblBillboard where Avenue=" + str1
						+ " and Betweenavenue like '%" + str2 + "%'", null);
		while (curs.isAfterLast()) {
			str3 = curs.getString(0);
			cur.moveToNext();
		}
		curs.close();
		Cursor curso = dbBill.rawQuery(
				"select * from tblbillboard where id > '" + str3
						+ "' and avenue= " + str2 + " limit 1", null);
		String str4 = "";
		String[] rowData = null;
		while (curso.isAfterLast()) {
			str4 = curso.getString(28);

			rowData = str4.split(" ");
		}
		return rowData[0].toString();

	}

	@Override
	public void onClick(View v) {
		Intent verificationIntent = new Intent(Verification.this,
				mainVerification.class);
		switch (v.getId()) {
		case R.id.btnGoForVerification:

			String text = spinner.getSelectedItem().toString();
			String StartingPointText = StartingPointspinner.getSelectedItem()
					.toString();

			verificationIntent.putExtra("AvenueName", text);
			verificationIntent.putExtra("StartingPoint", StartingPointText);
			verificationIntent.putExtra("position", selectedPosition);

			startActivityForResult(verificationIntent, 1);
			// startActivity(verificationIntent);
			break;
		case R.id.btnGoBackVerificationTables:
			Intent ourGoBackIntent = new Intent(Verification.this,
					MenuPage.class);
			startActivity(ourGoBackIntent);
			break;
		case R.id.chkIos:
			while (!((CheckBox) v).isChecked()) {
				String str1 = continueWithVerification(this.spinner
						.getSelectedItem().toString());
				verificationIntent.putExtra("AvenueName", this.spinner
						.getSelectedItem().toString());
				verificationIntent.putExtra("StartingPoint", str1);
				verificationIntent.putExtra("position", 1);
				startActivityForResult(verificationIntent, 1);
			}

			break;
		}

	}

	private void loadSpinner() {
		// here i used Set Because Set doesn't allow duplicates.
		Set<String> set = db.getAllData();

		List<String> list = new ArrayList<String>(set);
		Collections.sort(list);
		adapter = new ArrayAdapter<String>(Verification.this,
				android.R.layout.simple_spinner_item, list);

		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinner.setAdapter(adapter);
		spinner.setWillNotDraw(false);

		Set<String> set1 = db.getAllDataOnFirstTime();
		// showToast("You Selected Item :: " + name);

		List<String> listStartingPoint = new ArrayList<String>(set1);
		Collections.sort(listStartingPoint);
		adapterStartingPoint = new ArrayAdapter<String>(Verification.this,
				android.R.layout.simple_spinner_item, listStartingPoint);

		adapterStartingPoint
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		// StartingPointspinner.setT

		StartingPointspinner.setAdapter(adapterStartingPoint);
		// StartingPointspinner.setWillNotDraw(true);

		if (positionname != 0) {
			StartingPointspinner.setSelection(positionname);
			Toast.makeText(getApplicationContext(),
					"the pos is " + positionname, 20000).show();
		}

	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {
		String name = parent.getItemAtPosition(position).toString();
		Set<String> set = db.getAllDataForStartingPoint(name);
		showToast("You Selected Item :: " + name);

		List<String> listStartingPoint = new ArrayList<String>(set);
		Collections.sort(listStartingPoint);
		adapterStartingPoint = new ArrayAdapter<String>(Verification.this,
				android.R.layout.simple_spinner_item, listStartingPoint);

		adapterStartingPoint
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		StartingPointspinner.setAdapter(adapterStartingPoint);
		if (positionname != 0) {
			StartingPointspinner.setSelection(positionname);

		}

		StartingPointspinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int pos, long arg3) {
						// TODO Auto-generated method stub

						selectedPosition = pos;
						pref.edit()
								.putInt("selectedPosition", selectedPosition)
								.commit();
						// Toast.makeText(getApplicationContext(),
						// "in the click method"
						// +"postion is"+pos,2000).show();;

					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});

		StartingPointspinner.setWillNotDraw(false);

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}
}
